﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ITSTOREBLL;

namespace ITStore.Controllers
{ 
    public class ChiTietDonHangController : Controller
    {
        private ITStoreEntities db = new ITStoreEntities();

        //
        // GET: /ChiTietDonHang/

        public ViewResult Index()
        {
            var chitietdonhangs = db.ChiTietDonHangs.Include("DonHang").Include("SanPham");
            return View(chitietdonhangs.ToList());
        }

        //
        // GET: /ChiTietDonHang/Details/5

        public ViewResult Details(int id)
        {
            ChiTietDonHang chitietdonhang = db.ChiTietDonHangs.Single(c => c.ctdh_ID == id);
            return View(chitietdonhang);
        }

        //
        // GET: /ChiTietDonHang/Create

        public ActionResult Create()
        {
            ViewBag.ctdh_DonHang = new SelectList(db.DonHangs, "dh_ID", "dh_ID");
            ViewBag.ctdh_SanPham = new SelectList(db.SanPhams, "sp_ID", "sp_Ten");
            return View();
        } 

        //
        // POST: /ChiTietDonHang/Create

        [HttpPost]
        public ActionResult Create(ChiTietDonHang chitietdonhang)
        {
            if (ModelState.IsValid)
            {
                db.ChiTietDonHangs.AddObject(chitietdonhang);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.ctdh_DonHang = new SelectList(db.DonHangs, "dh_ID", "dh_ID", chitietdonhang.ctdh_DonHang);
            ViewBag.ctdh_SanPham = new SelectList(db.SanPhams, "sp_ID", "sp_Ten", chitietdonhang.ctdh_SanPham);
            return View(chitietdonhang);
        }
        
        //
        // GET: /ChiTietDonHang/Edit/5
 
        public ActionResult Edit(int id)
        {
            ChiTietDonHang chitietdonhang = db.ChiTietDonHangs.Single(c => c.ctdh_ID == id);
            ViewBag.ctdh_DonHang = new SelectList(db.DonHangs, "dh_ID", "dh_ID", chitietdonhang.ctdh_DonHang);
            ViewBag.ctdh_SanPham = new SelectList(db.SanPhams, "sp_ID", "sp_Ten", chitietdonhang.ctdh_SanPham);
            return View(chitietdonhang);
        }

        //
        // POST: /ChiTietDonHang/Edit/5

        [HttpPost]
        public ActionResult Edit(ChiTietDonHang chitietdonhang)
        {
            if (ModelState.IsValid)
            {
                db.ChiTietDonHangs.Attach(chitietdonhang);
                db.ObjectStateManager.ChangeObjectState(chitietdonhang, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ctdh_DonHang = new SelectList(db.DonHangs, "dh_ID", "dh_ID", chitietdonhang.ctdh_DonHang);
            ViewBag.ctdh_SanPham = new SelectList(db.SanPhams, "sp_ID", "sp_Ten", chitietdonhang.ctdh_SanPham);
            return View(chitietdonhang);
        }

        //
        // GET: /ChiTietDonHang/Delete/5
 
        public ActionResult Delete(int id)
        {
            ChiTietDonHang chitietdonhang = db.ChiTietDonHangs.Single(c => c.ctdh_ID == id);
            return View(chitietdonhang);
        }

        //
        // POST: /ChiTietDonHang/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            ChiTietDonHang chitietdonhang = db.ChiTietDonHangs.Single(c => c.ctdh_ID == id);
            db.ChiTietDonHangs.DeleteObject(chitietdonhang);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}